<?php


namespace App\Models;


use App\Services\ZlbService;
use Illuminate\Database\Eloquent\Model;

class ZlbLogistics extends Model
{
    protected $table = "order_logistics";
    protected $primaryKey = 'id';
    public $timestamps = false;

    /**
     * 订单发货物流上链新增数据
     * @param $order
     * @param $logic
     * @param $filePath
     */
    public static function addDeliver($order,$logic,$filePath){
        $add = [
            'logistics_name'=>$logic['logistics_name'],
            'logistics_no'=>$logic['logistics_no'],
            'deliver_time'=>dateFormat($logic['create_time']),
            'logistics_status'=>1,
            'deliver_url'=>$filePath,
            'deliver_hash'=>hash_file('sha256', $filePath),
        ];
        ZlbLogistics::where(['order_no'=>$order['trade_no']])->update($add);
    }

    /**
     * 订单发货物流上链
     * @param $order
     * @param $request
     * @param $notary
     */
    public static function updateDeliver($order,$request,$notary){
        $res = ZlbService::format($notary);
        if($res['status'] == ZlbService::STATUS_YES){
            //成功
            ZlbService::requestLog('上传订单物流发货成功',$request,$res);
        }else{
            //失败
            ZlbService::requestLog('上传订单物流发货失败',$request,$res);
        }
        $update = [
            'deliver_tx_hash'=>$res['tx_hash'],
            'chain_status'=>$res['status'],
            'chain_params'=>ZlbService::paramsResponse($request,$notary),
            'chain_time'=>dateFormat(time()),
        ];
        ZlbLogistics::where(['order_no'=>$order['trade_no']])->update($update);
    }

    /**
     * 订单确认收货物流新增数据
     * @param $order
     * @param $logic
     * @param $filePath
     */
    public static function addConfirm($order,$logic,$filePath){
        $update = [
            'confirm_url'=>$filePath,
            'confirm_hash'=>hash_file('sha256', $filePath),
            'logistics_status'=>3,
            'confirm_time'=>dateFormat($logic['confirm_time']),
        ];
        ZlbLogistics::where(['order_no'=>$order['trade_no']])->update($update);
    }

    /**
     * 订单物流签收上链
     * @param $order
     * @param $request
     * @param $notary
     */
    public static function updateConfirm($order,$request,$notary)
    {
        $res = ZlbService::format($notary);
        if($res['status'] == ZlbService::STATUS_YES){
            //成功
            ZlbService::requestLog('上传订单物流签收成功',$request,$res);
        }else{
            //失败
            ZlbService::requestLog('上传订单物流签收失败',$request,$res);
        }
        $update = [
            'confirm_tx_hash'=>$res['tx_hash'],
            'confirm_chain_status'=>$res['status'],
            'confirm_chain_params'=>ZlbService::paramsResponse($request,$notary),
            'confirm_chain_time'=>dateFormat(time()),
        ];
        ZlbLogistics::where(['order_no'=>$order['trade_no']])->update($update);
    }

}
